<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */
namespace App\Eloquent\Ygt;

use Framework\BaseClass\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use DB;
class OrderProcessCourse extends DbEloquent{
    use SoftDeletes;
    protected $dates = ['deleted_at'];
    protected $table    = 'ygt_order_process_course';

    public static function getFormsData( $arr, $limit=false, $offset=false )
    {
        $company_id         = $arr['company_id'];
        $start_time         = $arr['start_time'];
        $end_time           = $arr['end_time'];

        $select             = DB::raw('SUM(a.finish_number) AS finish_number_sum');
        $obj                = DB::table('ygt_order_process_course as a');
        $obj                = $obj->select( 'a.uid',$select );
        $obj                = $obj->leftJoin('ygt_department_user as b', 'a.uid', '=', 'b.user_id');
        $obj                = $obj->where('b.company_id', '=', $company_id);
        $obj                = $obj->where('a.created_at', '>', $start_time);
        $obj                = $obj->where('a.created_at', '<', $end_time);
        $obj                = $obj->groupBy('a.uid');
        $obj                = $obj->orderBy('finish_number_sum','desc');
        if( $limit !== false && $offset !== false )
        {
            $obj                = $obj->limit($limit);
            $obj                = $obj->offset($offset);
            $lists              = $obj->get();

            $obj                = DB::table('ygt_order_process_course as a');
            $obj                = $obj->select( 'a.uid',$select );
            $obj                = $obj->leftJoin('ygt_department_user as b', 'a.uid', '=', 'b.user_id');
            $obj                = $obj->where('b.company_id', '=', $company_id);
            $obj                = $obj->where('a.created_at', '>', $start_time);
            $obj                = $obj->where('a.created_at', '<', $end_time);
            $obj                = $obj->groupBy('a.uid');
            $count              = $obj->count();

            $result             = [ 'count'=>$count, 'lists'=>$lists ];
        } else {
            $result             = $obj->first();
        }
        return $result;
    }
    public static function getFormsOrderProessNumberList( $where_arr  )
    {
        $company_id         = $where_arr['company_id'];
        $start_time         = $where_arr['start_time'];
        $end_time           = $where_arr['end_time'];
        $user_id_arr        = $where_arr['user_id_arr'];

        $obj                = DB::table('ygt_order_process_course as a');
        $obj                = $obj->select( 'a.uid','a.order_process_id');
        $obj                = $obj->leftJoin('ygt_department_user as b', 'a.uid', '=', 'b.user_id');
        $obj                = $obj->where('b.company_id', '=', $company_id);
        $obj                = $obj->where('a.created_at', '>', $start_time);
        $obj                = $obj->where('a.created_at', '<', $end_time);
        $obj                = $obj->whereIn('a.uid', $user_id_arr);
        $result             = $obj->get();
        return $result;
    }
    public static function getAllWithUser($orderProcessId,$companyId){
        $re = self::where('order_process_id',$orderProcessId)->with(['user'=>function($query) use($companyId){
            $query->where('company_id',$companyId)->select('user_id','truename');
        }])->select([
            'id',
            'uid',
            'order_process_id',
            'total_number',
            'finish_number',
            'waste_number',
            'status',
            'assign_id',
            'created_at',
            'updated_at',
            'remark',
            'start_time',
            'estimated_time_minute',
        ])->get();

        return $re;

    }

    public function user(){
        return $this->belongsTo('App\Eloquent\Ygt\DepartmentUser','uid','user_id');
    }

}
